package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.PrivateMessage;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.PrivateMessageMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.service.IPrivateMessageService;
import cn.wolfcode.trip.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class PrivateMessageServiceImpl implements IPrivateMessageService {

    @Autowired
    private PrivateMessageMapper privateMessageMapper;

    @Autowired
    private UserMapper userMapper;
    Tag u = new Tag("b");

    @Override
    public void insert(PrivateMessage privateMessage) {
        u.setName("a");
        privateMessage.setSend(UserContext.getCurrentUser());
        privateMessage.setReceiveState(0);
        privateMessageMapper.insert(privateMessage);
    }

    @Override
    public List<PrivateMessage> selectContentBySendIdAndReceiveId(Long receiveId,Long sendId) {
        return privateMessageMapper.selectContentBySendIdAndReceiveId(receiveId,sendId);
    }

    @Override
    public List<PrivateMessage> selectByUserWeiDu(Long receiveId, Long sendId) {
        if (u.getName().equals("a")) {
            List<PrivateMessage> list = privateMessageMapper.selectByUserWeiDu(receiveId, sendId);
            u.setName("b");
            return list;
        }
            return null;
    }

    @Override
    public void changeReceiveState(Long id) {
        privateMessageMapper.changeReceiveState(id);
    }

    @Override
    public Integer selectPrivateNumberByUserId(Long id) {
        return privateMessageMapper.selectPrivateNumberByUserId(id);
    }

    @Override
    public void changePrivateTotal(Long sendId) {
        privateMessageMapper.changePrivateTotal(sendId,UserContext.getCurrentUser().getId());
    }


    @Override
    public Map<String,Object> selectlistPrivateUser() {
        List<PrivateMessage> list = privateMessageMapper.selectlistPrivateUser(UserContext.getCurrentUser().getId());
        Map<String,Object> map = new HashMap<>();
        ArrayList<Long> privateAndCurrentUserIds = new ArrayList<>();
        //查的是当前登录用户关注的user  id,可能为空
        //ArrayList<Long> beAttationIds =  userMapper.selectAttentionuserId(UserContext.getCurrentUser().getId());

        for (PrivateMessage m : list) {
            //发私信人的id
            privateAndCurrentUserIds.add(m.getSend().getId());
        }
            //加上当前用户
        privateAndCurrentUserIds.add(UserContext.getCurrentUser().getId());
        //查出关注了并且没发消息给当前用户的人
        List<User> users =  userMapper.selectByOtherUsers(privateAndCurrentUserIds,UserContext.getCurrentUser().getId());
        //发消息的用户
        map.put("sendUsers",list);
        //关注了但没发消息的用户
        map.put("otherUsers",users);
        return map;
    }


}
